package cn.mymall.web.controller.houtai.mall;

import cn.mymall.entity.mall.Category;
import cn.mymall.entity.mall.SubCategory;
import cn.mymall.service.mall.ICategoryService;
import cn.mymall.service.mall.ISubCategoryService;
import cn.mymall.support.ResultCode;
import cn.mymall.util.BaseWebUtil;
import cn.mymall.util.BindUtils;
import cn.mymall.util.DateUtil;
import cn.mymall.web.controller.houtai.BaseHoutaiController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class CategoryApiController extends BaseHoutaiController {
	@Autowired
	private ISubCategoryService subCategoryService;
	@Autowired
	private ICategoryService categoryService;


	// 分类列表
	@RequestMapping("/api/category/list")
	public ResultCode<Map> categoryList(Integer pagenum, Integer pagesize, String name) {
		if (pagenum == null) {
			pagenum = 1;
		}
		if (pagesize == null) {
			pagesize = 10;
		}
		QueryWrapper ew = new QueryWrapper<Category>();
		if(StringUtils.isNotBlank(name)){
			ew.like("name",name);
		}

		Page pageParams = new Page(pagenum,pagesize);
		Page<Category> page = categoryService.page(pageParams,ew);
		List<Category> categoryList = page.getRecords();
		Map<String, Integer> subCategoryCountMap = new HashMap<String, Integer>();
		for(Category category : categoryList){
			int count = subCategoryService.count(new QueryWrapper<SubCategory>().eq("category_id", category.getId()));
			subCategoryCountMap.put(category.getId()+"", count);
		}
		Map map = new HashMap();
		map.put("subCategoryCountMap", subCategoryCountMap);
		map.put("categoryList", categoryList);
		map.put("page", ImmutableMap.of("total", page.getTotal(), "pagenum", page.getCurrent()));
		return ResultCode.getSuccessReturn(map);
	}

	// 分类新增or修改
	@RequestMapping("/api/category/get")
	public ResultCode modifyCategory(Integer id) {
		Category category = new Category();
		if (id != null) {
			category = categoryService.getById(id);
		}
		return ResultCode.getSuccessReturn(category);
	}

	// 分类保存
	@RequestMapping("/api/category/save")
	public ResultCode saveCategory(Integer id) {
		Category category = null;
		if (id != null) {
			category = categoryService.getById(id);
		} else {
			category = new Category();
			category.setAddTime(DateUtil.getCurFullTimestamp());
		}
		BindUtils.bindData(category, BaseWebUtil.getRequestParams());
		if(StringUtils.isBlank(category.getName())) {
			return ResultCode.getFailure("请输入分类名称");
		}
		if(category.getSortNum()==null) {
			return ResultCode.getFailure("请输入排序");
		}
		category.setUpdateTime(DateUtil.getCurFullTimestamp());
		categoryService.saveOrUpdate(category);
		return ResultCode.SUCCESS;
	}

	@RequestMapping("/api/category/setSortNum")
	public ResultCode modifyCategory(Integer id, Integer sortNum) {
		Category category = categoryService.getById(id);
		category.setSortNum(sortNum);
		categoryService.saveOrUpdate(category);
		return ResultCode.getSuccessReturn(category);
	}
}
